package com.konne.nightmare.FastPublicOpinion.ui.information.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.BarUtils;
import com.google.android.material.tabs.TabLayout;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpActivity;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpPresenter;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.ClassAddActivePopupWindow;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.MyFragmentPagerAdapter;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassAllFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassFiveFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassFourFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassOneFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassSevenFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassSixFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassThreeFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.ClassTwoFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.EventFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.HomeFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.OpinionFragment;
import com.konne.nightmare.FastPublicOpinion.ui.information.fragment.UserFragment;
import com.konne.nightmare.FastPublicOpinion.ui.personal.adapter.HelpAdapter;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.konne.nightmare.FastPublicOpinion.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author: chen
 * @UpdateDate: 1/24/22
 * @UpdateRemark:
 */
public class ClassDetailActivity extends BaseMvpActivity implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    List<String> labels = new ArrayList<>();

    private List<Fragment> fragments = new ArrayList<>();
    ClassAllFragment classAllFragment ;
    ClassOneFragment classOneFragment ;
    ClassTwoFragment classTwoFragment ;
    ClassThreeFragment classThreeFragment ;
    ClassFourFragment classFourFragment;
    ClassFiveFragment classFiveFragment;
    ClassSixFragment classSixFragment ;
    ClassSevenFragment classSevenFragment;
    @Override
    protected BaseMvpPresenter createPresenter() {
        return new BaseMvpPresenter();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_class_detail;
    }

    @Override
    public void initView() {
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
        imgRight.setVisibility(View.VISIBLE);
//        initImmersionBar(R.color.white,true);
        tvTitle.setText("数学一班");
        labels.add("全部");
        labels.add("课堂目录");
        labels.add("作业");
        labels.add("考试");
        labels.add("讨论");
        labels.add("课堂互动");
        labels.add("腾讯会议");
        labels.add("资料库");
        for (int i = 0; i < labels.size(); i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = LayoutInflater.from(this).inflate(R.layout.tab_labes_layout, null);
            TextView textView = view.findViewById(R.id.tabLabel);
            textView.setText(labels.get(i));
            tab.setCustomView(view);
            tabLayout.addTab(tab, false);
        }


        fragments.add(classAllFragment);
        fragments.add(classOneFragment);
        fragments.add(classTwoFragment);
        fragments.add(classThreeFragment);
        fragments.add(classFourFragment);
        fragments.add(classFiveFragment);
        fragments.add(classSixFragment);
        fragments.add(classSevenFragment);
//        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);
//        viewPager.setAdapter(myFragmentPagerAdapter);
//        viewPager.setNoScroll(false);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView)view).setTextColor(ContextCompat.getColor(ClassDetailActivity.this,R.color.white));
                    ((TextView)view).setBackgroundResource(R.drawable.bg_tab_text_selector);
                }
                //开启事务
                mTransaction = mManager.beginTransaction();

                hideFragment(mTransaction);

                /**
                 * fragment 用 add + show + hide 方式
                 * 只有第一次切换会创建fragment，再次切换不创建
                 *
                 * fragment 用 replace 方式
                 * 每次切换都会重新创建
                 *
                 */
                switch (tab.getPosition()) {
                    case 0:
                        if (classAllFragment == null) {
                            classAllFragment = new ClassAllFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classAllFragment);

                        } else {
                            mTransaction.show(classAllFragment);
                        }
                        break;
                    case 1:

                        if (classOneFragment == null) {
                            classOneFragment = new ClassOneFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classOneFragment);

                        } else {
                            mTransaction.show(classOneFragment);
                        }
                        break;
                    case 2:
                        if (classTwoFragment == null) {
                            classTwoFragment = new ClassTwoFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classTwoFragment);

                        } else {
                            mTransaction.show(classTwoFragment);
                        }
                        break;
                    case 3:
                        if (classThreeFragment == null) {
                            classThreeFragment = new ClassThreeFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classThreeFragment);

                        } else {
                            mTransaction.show(classThreeFragment);
                        }
                        break;

                    case 4:

                        if (classFourFragment == null) {
                            classFourFragment = new ClassFourFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classFourFragment);

                        } else {
                            mTransaction.show(classFourFragment);
                        }
                        break;
                    case 5:

                        if (classFiveFragment == null) {
                            classFiveFragment = new ClassFiveFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classFiveFragment);

                        } else {
                            mTransaction.show(classFiveFragment);
                        }
                        break;
                    case 6:

                        if (classSixFragment == null) {
                            classSixFragment = new ClassSixFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classSixFragment);

                        } else {
                            mTransaction.show(classSixFragment);
                        }
                        break;
                    case 7:

                        if (classSevenFragment == null) {
                            classSevenFragment = new ClassSevenFragment();
                            mTransaction.add(R.id.frameLayout,
                                    classSevenFragment);

                        } else {
                            mTransaction.show(classSevenFragment);
                        }
                        break;

                }
                // 事务提交
                mTransaction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView)view).setTextColor(ContextCompat.getColor(ClassDetailActivity.this,R.color.color_1C2B33));
                    ((TextView)view).setBackgroundResource(R.drawable.bg_tab_text_unselect);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mManager = getSupportFragmentManager();
        tabLayout.getTabAt(0).select() ;
    }

    @OnClick({R.id.img_back,R.id.img_add_active})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_add_active:
                ClassAddActivePopupWindow classAddActivePopupWindow = new ClassAddActivePopupWindow(ClassDetailActivity.this);
                classAddActivePopupWindow.showAtLocation(frameLayout, Gravity.BOTTOM, 0, 0);
                break;


        }
    }

    /**
     * 隐藏当前fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (classAllFragment != null) {
            transaction.hide(classAllFragment);
        }

        if (classOneFragment != null) {
            transaction.hide(classOneFragment);
        }

        if (classTwoFragment != null) {
            transaction.hide(classTwoFragment);
        }
        if (classThreeFragment != null) {
            transaction.hide(classThreeFragment);
        }
        if (classFourFragment != null) {
            transaction.hide(classFourFragment);
        }
        if (classFiveFragment != null) {
            transaction.hide(classFiveFragment);
        }
        if (classSixFragment != null) {
            transaction.hide(classSixFragment);
        }
        if (classSevenFragment != null) {
            transaction.hide(classSevenFragment);
        }
    }
    @Override
    public LoadProgressDialog getLoadDialog() {
        return dialog;
    }

    @Override
    public void cancelLoadDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.cancel();
        }
    }
}


